#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int subarraysDivByK(vector<int>& nums, int k) {
        unordered_map<int, int> m;
        m[0 % k] = 1;

        int sum = 0, countK = 0;
        for (auto x : nums)
        {
            sum += x;
            int a = (sum % k + k) % k;
            if (m.count(a))
                countK += m[a];
            m[a]++;
        }
        return countK;
    }
};